package com.etocrm.saas.lib.base.resource.Util;

import com.etocrm.saas.lib.base.resource.model.ThirdUserVO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


public class ThirdPaytyOauth2Util {

    public static ThirdUserVO getThirdPaytyOUserJwtFromHeader(HttpServletRequest request){
    	//获取JWT中的内容部分
        Map<String, String> jwtClaims = Oauth2Util.getJwtClaimsFromHeader(request);
        
        if(jwtClaims == null || StringUtils.isEmpty(jwtClaims.get("id"))){
            return null;
        }
        ThirdUserVO thirdUserVO = new ThirdUserVO();
        thirdUserVO.setId(Long.valueOf(jwtClaims.get("id")));
        thirdUserVO.setUserName(jwtClaims.get("username"));
        thirdUserVO.setCallerName(jwtClaims.get("callerName"));
        thirdUserVO.setAppKey(jwtClaims.get("appKey"));
        thirdUserVO.setContacts(jwtClaims.get("contacts"));
        thirdUserVO.setTel(jwtClaims.get("tel"));
        thirdUserVO.setCheckExp(StringUtils.isNotEmpty(jwtClaims.get("checkExp")) ?Integer.valueOf(jwtClaims.get("checkExp")):null);
        thirdUserVO.setExpStartAt(StringUtils.isNotEmpty(jwtClaims.get("expStartAt")) ?new Date(jwtClaims.get("expStartAt")) :null);
        thirdUserVO.setExpEndAt(StringUtils.isNotEmpty(jwtClaims.get("expEndAt")) ?new Date(jwtClaims.get("expEndAt")) :null);
        thirdUserVO.setCheckIp(StringUtils.isNotEmpty(jwtClaims.get("checkIp")) ?Integer.valueOf(jwtClaims.get("checkIp")):null);
        return thirdUserVO;
    }


}
